package com.itboye.sunsunhome.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;

/***
 * 自定义进度
 * 
 * @author itboye
 * 
 */
public class ProcessImageView extends Imagus {

	private Paint mPaint;// 画笔
	int width = 0;
	int height = 0;
	Context context = null;
	int progress = 0;
	private Matrix matrix = new Matrix();;

	public ProcessImageView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mPaint.setAntiAlias(true); // 消除锯齿
	}

	public ProcessImageView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		mPaint.setAntiAlias(true); // 消除锯齿
	}

	public ProcessImageView(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		this.context = context;
		mPaint = new Paint();
		mPaint.setAntiAlias(true); // 消除锯齿
		matrix.setRotate(30);
		matrix.postScale(0.5f, 0.5f);
	}

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		mPaint.setAntiAlias(true); // 消除锯齿
		mPaint.setStyle(Paint.Style.FILL);

		mPaint.setColor(Color.parseColor("#70000000"));// 半透明
		canvas.drawRect(0, 0, getWidth(), getHeight() - getHeight() * progress
				/ 100, mPaint);

		mPaint.setColor(Color.parseColor("#00000000"));// 全透明
		canvas.drawRect(0, getHeight() - getHeight() * progress / 100,
				getWidth(), getHeight(), mPaint);

		mPaint.setTextSize(40);
		mPaint.setColor(Color.parseColor("#FFFFFF"));
		mPaint.setStrokeWidth(2);
		Rect rect = new Rect();
		mPaint.getTextBounds("100%", 0, "100%".length(), rect);// 确定文字的宽度
		canvas.drawText("设备连接中" + progress + "%",
				(getWidth() / 2 - rect.width() / 2) - 150, getHeight() - 50,
				mPaint);
	}

	public void setProgress(int progress) {
		this.progress = progress;
		postInvalidate();
	};

	/**
	 * 获取圆角矩形图片方法
	 * 
	 * @param bitmap
	 * @param roundPx
	 *            ,一般设置成14
	 * @return Bitmap
	 * @author caizhiming
	 */
	private Bitmap getRoundBitmap(Bitmap bitmap, int roundPx) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;

		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		mPaint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		mPaint.setColor(color);
		int x = bitmap.getWidth();

		canvas.drawRoundRect(rectF, roundPx, roundPx, mPaint);
		mPaint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, mPaint);
		return output;

	}
}
